package com.mxpio.erp.mes.controller;


import com.mxpio.erp.common.mes.entity.ManufactureWorkOrderTask;
import com.mxpio.erp.common.mes.entity.MesScanInfo;
import com.mxpio.erp.common.mes.service.ManufactureWorkOrderTaskService;
import com.mxpio.erp.common.mes.service.MesScanInfoService;
import com.mxpio.erp.common.mes.vo.ScanInfoVo;
import com.mxpio.erp.common.technology.entity.WorkShop;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "MesScanInfoController", description = "生产操作信息接口")
@RestController
@RequestMapping("/erp/mes/scan_info/")
public class MesScanInfoController {

    @Autowired
    private MesScanInfoService service;

    @Autowired
    private ManufactureWorkOrderTaskService manufactureWorkOrderTaskService;

    @GetMapping("list")
    @Operation(summary = "生产操作信息列表", description = "获取生产操作信息列表（无分页）", method = "GET")
    public Result<List<MesScanInfo>> list(@Parameter(description="查询参数") Criteria criteria)  {
        List<MesScanInfo> lines = service.list(MesScanInfo.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "生产操作信息列表", description = "获取生产操作信息列表（分页）", method = "GET")
    public Result<Page<MesScanInfo>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<MesScanInfo> lines = service.listPage(MesScanInfo.class, page, criteria);
        return Result.OK(lines);
    }

    @Operation(summary = "登录机台-界面展示车间工位、工作中心、作业单元数据源", description = "登录机台-界面展示车间工位、工作中心、作业单元数据源，前端通过车间、工作中心、作业单元一层一层取值", method = "GET")
    @GetMapping(value = "load_work_unit/{procMode}")
    public Result<List<WorkShop>> loadWorkUnit(@PathVariable(name = "procMode", required = true) String procMode) {
        return service.loadWorkUnit(procMode);
    }

    @Operation(summary = "选中作业单元后，加载对应的工单工序任务列表，默认按创建时间升序排序", description = "选中作业单元后，加载对应的工单工序任务列表，默认按创建时间升序排序", method = "GET")
    @GetMapping(value = "get_wo_task/list/{workUnitCode}")
    public Result<List<ManufactureWorkOrderTask>> getWorkOrderTaskList(@PathVariable(name = "workUnitCode", required = true) String workUnitCode) {
        return service.getWorkOrderTaskList(workUnitCode);
    }

    @Operation(summary = "加载工单（批次）信息（装配扫批次条码）", description = "加载工单（批次）信息（装配扫批次条码）", method = "POST")
    @PostMapping(value = "load_work_order")
    //@Deprecated
    public Result<ScanInfoVo> loadWorkOrder(@RequestBody MesScanInfo operateInfo) {
        return service.loadWorkOrderTaskByBatchCode(operateInfo);
    }

    @Operation(summary = "根据批次条码加载订单信息", description = "根据批次条码加载订单信息", method = "POST")
    @PostMapping(value = "load_mo")
    public Result<ScanInfoVo> loadMOByBatchCode(@RequestBody ScanInfoVo operateInfo) {
        return service.loadManufactureOrderByBatchCode(operateInfo);
    }

    @Operation(summary = "加载工单工序任务", description = "加载工单工序任务", method = "POST")
    @PostMapping(value = "load_work_order_task")
    public Result<ScanInfoVo> loadWorkOrderTask(@RequestBody MesScanInfo operateInfo, HttpServletRequest request) {
        //String ip = request.getHeader();
        return service.loadWorkOrderTask(operateInfo);
    }

    @Operation(summary="现场管理-装配批次扫描报工", description="现场管理-装配批次扫描报工")
    @PostMapping(value = "scan/zp")
    public Result<ScanInfoVo> scanAssemble(@RequestBody ScanInfoVo scanInfoVO) {
        return service.scanAssemble(scanInfoVO);
    }
    @Operation(summary="现场管理-机加批次扫描报工", description="现场管理-机加批次扫描报工")
    @PostMapping(value = "scan/jj")
    public Result<ScanInfoVo> scanMachine(@RequestBody ScanInfoVo scanInfoVO) {
        return service.scanMachine(scanInfoVO);
    }
}
